package com.juice.iot.sdk.mqtt;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

/**
 * Created by haonan on 2018/11/28
 */
public class MqttFactory {

    public MqttClient getMqttClient(String uri,String deviceCode,String secret) throws MqttException {
        MemoryPersistence persistence = new MemoryPersistence();
        MqttClient sampleClient = new MqttClient(uri, "master-"+deviceCode, persistence);
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setCleanSession(true);
        connOpts.setKeepAliveInterval(5);
        connOpts.setUserName(deviceCode);
        connOpts.setPassword(secret.toCharArray());
        sampleClient.connect(connOpts);
        return sampleClient;
    }

}
